package com.itheima.test6;

public class StudentTest2 {
    public static void main(String[] args) {
        //要求5:查询数组id为“heima002”的学生，如果存在，则将他的年龄+1岁
        Student[] arr = new Student[3];

        Student st1 = new Student(1,"张三",20);
        Student st2 = new Student(2,"李四",23);
        Student st3 = new Student(3,"王五",21);

        arr[0] = st1;
        arr[1] = st2;
        arr[2] = st3;

        //获取索引
        int index = getIndex(arr,2);
        if (index >= 0){
            Student st = arr[index];
            int newAge = st.getAge() + 1;
            st.setAge(newAge);
            print(arr);
        }else {
            System.out.println("该学生不存在");
        }

    }
    public static void print(Student[] arr){
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null){
                Student st = arr[i];
                System.out.println(st.getId() + "," + st.getName() + "," + st.getAge());
            }
        }
    }
    public static int getIndex(Student[] arr,int id){
        int index= -1;

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != null){
                Student st = arr[i];
                if (st.getId() == id){
                    index = i;
                }
            }
        }


        return index;
    }
}
